package com.hbnu.filter;

import com.hbnu.pojo.User;
import com.hbnu.service.UserService;
import com.hbnu.service.impl.UserServiceImpl;
import com.hbnu.util.CookieUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author chendikai
 * @date 2024-11-13 上午 10:40
 */
@WebFilter("/*")
public class LoginFilter implements Filter {

    private UserService userService = new UserServiceImpl();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        User sessionUser = (User) request.getSession().getAttribute("session_user");
        Cookie cookie = CookieUtils.getCookieByCookieName("cookie_stu_pass", request);
        String requestURI = request.getRequestURI();  // 如http://localhost:8080/index.jsp,requestURI表示的是/index.jsp

        if (sessionUser != null) {  // 表示用户已经登录过系统
            if (cookie != null) {  // 表示用户已经勾选了“记住我”
                if (requestURI.equals("/") || requestURI.equals("/login")) {
                    request.getSession().setAttribute("session_user", sessionUser);
                    request.getRequestDispatcher("/jsp/main.jsp").forward(servletRequest, servletResponse);
                } else {
                    filterChain.doFilter(servletRequest, servletResponse);  // 放行
                }
            } else {
                filterChain.doFilter(servletRequest, servletResponse);  // 放行
            }
        } else {
            if (cookie != null) {  // 用户未登录，之前勾选了“记住我”
                String cookieValue = cookie.getValue();
                String[] cookieValues = cookieValue.split("#");

                User user = userService.findUserByStuCodeAndPassword(cookieValues[0], cookieValues[1]);

                if (requestURI.equals("/") || requestURI.equals("/login")) {
                    request.getSession().setAttribute("session_user", user);
                    request.getRequestDispatcher("/jsp/main.jsp").forward(servletRequest, servletResponse);
                } else {
                    filterChain.doFilter(servletRequest, servletResponse);  // 放行
                }
            } else {  // 用户未登录，之前也未勾选“记住我”
                if (requestURI.equals("/") || requestURI.equals("/login")) {
                    filterChain.doFilter(servletRequest, servletResponse);
                } else {
                    request.setAttribute("error", "您还未登录，请您登录后再访问");
                    request.getRequestDispatcher("index.jsp").forward(servletRequest, servletResponse);
                }
            }
        }
    }

    @Override
    public void destroy() {

    }
}
